<!--
 * @Description: 单位管理页面
-->
<template>
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="96"
        :add-active="addActive"
        :del-active="delActive"
        :has-edit="false"
        :has-save="false"
        :has-cancel="false"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:tab-select>
      <SelectButton
        :buttonselectlist="buttonselectlist"
        @nowselectbuttonindex="batchstaumet"
      />
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="productNo" label="产品编码" />
        <el-table-column prop="productName" label="产品名称" />
        <el-table-column prop="specification" label="规格">
          <template
            v-if="scope.row.productNo"
            slot-scope="scope"
          >{{ scope.row.specification }} {{ scope.row.perSpecUnitName }}/{{ scope.row.unitName }}</template>
        </el-table-column>
        <el-table-column prop="model" label="型号" />
        <el-table-column prop="remarks" label="备注" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.productNo" slot-scope="scope">
            <el-button
              v-if="judgePermission(96, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(96, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '单位信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          class="el-form"
        >
          <el-form-item label="产品编码:" prop="productNo" class="w100p">
            <el-input v-model="formData.productNo" clearable />
          </el-form-item>
          <el-form-item label="产品名称:" prop="productName" class="w100p">
            <el-input v-model="formData.productName" clearable />
          </el-form-item>
          <el-form-item label="规格:" prop="specification" class="w50p">
            <el-input v-model="formData.specification" clearable />
          </el-form-item>
          <el-form-item label="单位:" class="w50p">
            <el-form-item class="w50p">
              <el-select v-model="formData.perSpecUnitNo" filterable clearable>
                <el-option
                  v-for="item in unitList"
                  :key="item.unitNo"
                  :label="item.unitName"
                  :value="item.unitNo+''"
                />
              </el-select>
            </el-form-item>
            <span style="padding:0 3px">/</span>
            <el-form-item class="w50p">
              <el-select v-model="formData.unitNo" filterable clearable>
                <el-option
                  v-for="item in unitList"
                  :key="item.unitNo"
                  :label="item.unitName"
                  :value="item.unitNo+''"
                />
              </el-select>
            </el-form-item>
          </el-form-item>
          <el-form-item label="型号:" prop="model" class="w100p">
            <el-input v-model="formData.model" clearable />
          </el-form-item>
          <el-form-item label="备注:" prop="remarks" class="w100p">
            <el-input v-model="formData.remarks" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/product-manage";
import unitApi from "@/api/basic-info-manage/unit-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixin";
import SelectButton from "@/components/selectbutton/index.vue";
import { mapGetters } from "vuex";
export default {
  name: "ProductManagement",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot, SelectButton },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '产品编码',
          prop: 'productNo',
          type: 'input',
        }, {
          label: '产品名称',
          prop: 'productName',
          type: 'input',
        }, {
          label: '规格',
          prop: 'specification',
          type: 'input',
        }, {
          label: '型号',
          prop: 'model',
          type: 'input',
        }, 
      ],
      searchlist: {},
      unitList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      buttonselectlist: [{ name: "成品" }, { name: "半成品" }],
      curTabIndex: 0
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.getunitList();
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() {},
  methods: {
    batchstaumet(index) {
      this.curTabIndex = index;
      this.getTableDataList();
    },
    getunitList() {
      let params = {};
      unitApi.getallBasicunitlist(params).then(res => {
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        this.unitList = data;
      });
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          productNo: this.searchlist.productNo,
          productName: this.searchlist.productName,
          specification: this.searchlist.specification,
          model: this.searchlist.model,
          productType: this.buttonselectlist[this.curTabIndex]?.name
        }
      };
      nowpagereq.queryPageByCondition(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
      // }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    async handleEdit(row) {
      if (row.productNo) {
        this.openDialog("edit");
        this.formData = JSON.parse(JSON.stringify(row));
      }
    },
    // 添加
    add() {
      this.openDialog("add");
      this.formData = {};
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {};
    },
    // 初始化操作栏状态
    resetActionBar() {
      this.nowChanceStau = "";
      this.formData = {};
    },
    handleSelectionChange(list) {
      this.selectedPlantingUnitList = [];
      // let notvalue = true
      this.selectedPlantingUnitList = list.map(item => {
        if (item.unitNo) {
          return item.unitNo;
        } else {
          // notvalue = false
          return "";
        }
      });
    },
    delmessage() {
      nowpagereq.delList(this.selectedPlantingUnitList).then(res => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.getTableDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(data) {
      // nowpagereq.delList([data.productNo]).then(res => {
      //   if (res.code === 1) {
      //     this.$message.success('删除成功')
      //     this.getTableDataList();
      //   } else {
      //     this.$message.error('删除失败')
      //   }
      // })
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          nowpagereq.delList([data.productNo]).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.getTableDataList();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        nowpagereq.edit(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        // 新增接口
        let params = JSON.parse(JSON.stringify(this.formData));
        params.productType = this.buttonselectlist[this.curTabIndex]?.name;
        nowpagereq.add(params).then(res => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss">
.df {
  display: flex;
  align-items: center;
  width: 100%;
}

.dfs {
  ::v-deep .el-form-item__content {
    flex-direction: column;
  }
  i {
    font-size: 30px;
    margin-left: 10px;
    cursor: pointer;
  }
}

.tags {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .tag-item {
    height: 30px;
    padding: 5px 10px;
    background-color: #00a1c2;
    border-radius: 8px;
    display: flex;
    align-items: center;
    margin-top: 10px;
    margin-right: 10px;

    i {
      font-size: 14px;
    }
  }
}
</style>
